package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //新增套餐
    @Override
    public void savewithDish(SetmealDTO setmealDTO) {
        //将前端页面传递过来的数据保存到setmeal表中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //将页面传递过来的菜品数据保存到setmeal_dish表中
        //首先获取套餐id  因为前端页面没有套餐id
        Long id = setmeal.getId();
        //获取前端页面传递过来的菜品的口味数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //此时还不能将数据插入到数据库中，因为此时setmealDishes中的每个setmealDish对象中都没有套餐id
        //先判断setmealDish是否为空后遍历，给setmealDishes中的每个setmealDish设置套餐id
        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish ->{
                setmealDish.setSetmealId(id);
                    });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    //套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //起售停售套餐
    @Override
    public void status(Integer status, Long id) {
        setmealMapper.status(status,id);
    }

    //根据套餐id查询套餐数据
    @Override
    public SetmealVO getByIdwithDish(Long id) {
        //首先要查基本的套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //其次要查套餐对应的菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        //先修改基本的Setmeal中的东西
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //在修改SetmealDish中的东西
        //首先先删除SetmealDish表中的菜品数据
        setmealDishMapper.deleteById(setmealDTO.getId());
        //再添加新的套餐中的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    //批量删除套餐
    @Override
    public void deleteById(List<Long> ids) {
        //判断套餐是否起售
        for(Long id:ids){
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //批量删除套餐表中的数据
        setmealMapper.deleteByIds(ids);
        //批量删除套餐菜品表中的数据
        setmealDishMapper.deleteByIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
